home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Moscow ML 1.42 / examples / lexyacc / cl / lists.cl < prev    next >
Encoding:
Text File  |  1997-08-18  |  2.5 KB  |  81 lines  |  [TEXT/R*ch]

  1. (* An unsystematic collection of list utility functions *)
  2.  
  3.    ones = pack{2, 1, ones};
  4.  
  5.    take = \n.\xs.case xs of
  6.                  <1>      -> pack {1} ;
  7.                  <2> x xr -> if n=0 then pack {1}
  8.                              else pack {2, x, take (n-1) xr}
  9.                  end;
  10.  
  11.    filter = \p.\xs.case xs of
  12.                    <1>      -> pack {1} ;
  13.                    <2> x xs -> if p x then pack {2, x, filter p xs}
  14.                                else filter p xs
  15.                    end ;
  16.  
  17.    from = \n.pack {2, n, from (n+1)}
  18.  
  19.   map = \f.\xs. case xs of
  20.                   <1>      -> pack{1};
  21.                   <2> x xr -> pack{2, f x, map f xr}
  22.                 end; 
  23.  
  24.   head = \xs. case xs of
  25.                 <1>      -> 0;
  26.                 <2> x xr -> x
  27.               end;
  28.  
  29.   tail = \xs. case xs of
  30.                 <1>      -> pack{1};
  31.                 <2> x xr -> xr
  32.               end;
  33.  
  34.   zip = \xs.\ys.
  35.         case xs of
  36.           <1>      -> pack{1};
  37.           <2> x xr -> case ys of
  38.                         <1>      -> pack{1};
  39.                         <2> y yr -> pack{2, pack{1, x, y}, zip xr yr}
  40.                   end
  41.         end;
  42.  
  43.   foldr = \f.\z.\xs.
  44.           case xs of
  45.             <1>      -> z;
  46.             <2> x xr -> f x (foldr f z xr)
  47.           end; 
  48.  
  49.   iterate = \f.\x. pack{2, x, iterate f (f x)};
  50.  
  51.   repeat = \x.letrec xs = pack{2, x, xs} in xs;
  52.  
  53.   append = \xs.\ys. case xs of
  54.                   <1>      -> ys;
  55.                       <2> x xr -> pack{2, x, append xr ys}
  56.                     end;
  57.  
  58.   (* The following version of scanr works only for finite lists *)
  59.  
  60.   scanr = \f.\q0.letrec h = \xs. case xs of
  61.                                    <1>      -> pack{2, q0, pack{1}};
  62.                                    <2> x xr -> case h xr of
  63.                                                  <1>      -> pack{1};
  64.                                                  <2> q qs -> pack{2, f x q, qs}
  65.                                                end
  66.                                  end
  67.                  in h
  68.  
  69.   (* This alternative version works for infinite lists but leaks space
  70.      because of Wadler/Sparud's problem *)
  71.  
  72.   scanr = \f.\q0.letrec h = \xs. case xs of
  73.                                    <1>      -> pack{2, q0, pack{1}};
  74.                                    <2> x xr -> letrec qqs = h xr 
  75.                                                       q   = head qqs
  76.                                                       qs  = tail qqs
  77.                            in pack{2, f x q, qs}
  78.                                  end
  79.                                  end
  80.                  in h
  81.